package com.iss.umservice.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.iss.iframework.security.OperatorUserEntry;
import com.iss.iframework.util.DateUtil;
import com.iss.iframework.util.StringUtil;
import com.iss.iframework.web.ResultCall;
import com.iss.iframework.web.support.tld.pagination.Navigation;
import com.iss.iframework.web.support.tld.pagination.PageUtil;
import com.iss.iframework.web.support.tld.pagination.Pagination;

import com.iss.umservice.bean.ServiceApplyBean;
import com.iss.umservice.bean.ServiceBean;
import com.iss.umservice.bean.TutorBean;
import com.iss.umservice.bean.UserAccount;
import com.iss.umservice.common.Week;

import com.iss.umservice.manager.AccountManager;
import com.iss.umservice.manager.ServiceManager;
import com.iss.umservice.manager.TutorManager;

/**
 * 锟斤拷锟斤拷锟斤拷锟斤拷源锟借定
 * 
 * @author Administrator
 * 
 */

@Controller
@RequestMapping("/servicecreate/*")
@SessionAttributes("curUser")
public class ServiceCreateController {
	@Autowired
	private ServiceManager serviceManager;

	@Autowired
	private AccountManager accountManager;
	
	@Autowired
	private TutorManager tutorManager;
	

	/**
	 * 锟斤拷询锟斤拷锟斤拷锟斤拷息
	 */
	@RequestMapping
	public String queryServiceInfo(ServiceBean serviceBean2, String pages, Map<String, Object> model) {
		ServiceBean serviceBean = serviceManager.queryServiceInfo(serviceBean2.getServiceID());
		model.put("serviceBean", serviceBean);
		model.put("serviceBean2", serviceBean2);
		model.put("type", serviceBean2.getType());
		model.put("pages", pages);
		return "service/queryserviceinfo";
	}

	/**
	 * 锟斤拷询锟斤拷锟斤拷锟叫憋拷
	 * @throws UnsupportedEncodingException 
	 */

	@RequestMapping
	public String queryServiceList(
			@ModelAttribute("curUser") OperatorUserEntry userEntry,
			ServiceBean serviceBean, String pages, Map<String, Object> model) throws UnsupportedEncodingException {
		String userType= accountManager.queryUserType(userEntry.userIdentifier());
		// 锟斤拷锟斤拷锟斤拷锟揭碉拷没锟�只锟缴诧拷询锟斤拷状态锟侥凤拷锟斤拷锟斤拷息
		if("2".equals(userType))
		{
			serviceBean.setStatus("1");
		}
		
        if(serviceBean.getServiceName()!=null&&serviceBean.getServiceName().equals(new String( serviceBean.getServiceName().getBytes("ISO8859_1"),"ISO8859_1")))
        {
        	serviceBean.setServiceName( new String( serviceBean.getServiceName().getBytes("ISO8859_1"),"UTF-8"));
        }
		
		int count = serviceManager.countServiceInfo(serviceBean);

		Pagination pagination = PageUtil.createPage(pages, count,
				"../servicecreate/queryServiceList.html");

		// 锟接会话锟叫伙拷取锟矫伙拷锟斤拷锟斤拷
		List<ServiceBean> serviceList = serviceManager.queryServiceList(
				serviceBean, pagination.getOffset(), pagination.getRowSize());

		// 锟斤拷页锟斤拷锟斤拷
		pagination.addCondition("serviceName", serviceBean.getServiceName());
		pagination.addCondition("serviceType", serviceBean.getServiceType());
		pagination.addCondition("status", serviceBean.getStatus());
		pagination.addCondition("createDate", serviceBean.getCreateDate());
		pagination.addCondition("type", serviceBean.getType());

		model.put("serviceList", serviceList);
		model.put("serviceBean", serviceBean);
		model.put(Navigation.PAGINATION, pagination);
		model.put("type", serviceBean.getType());
		model.put("userType", userType);
		model.put("pages", pages);
		return "service/servicelist";
	}

	/**
	 * 锟斤拷转锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷页锟斤拷
	 */
	@RequestMapping
	public String gotoCreateService(String type,Map<String, Object> model) {
		model.put("opType", "1");
		model.put("type", type);
		return "service/createservice";

	}
	
	@RequestMapping
	public String refuse() {
		return "service/refuse";

	}

	/**
	 * 锟斤拷锟斤拷锟斤拷锟斤拷
	 * 
	 * @param servicebean
	 */
	@RequestMapping
	public void createService(
			@ModelAttribute("curUser") OperatorUserEntry userEntry,
			ServiceBean serviceBean) {
		serviceBean.setServiceID(StringUtil.getPrimarySessionId());
		serviceBean.setCreateDate(DateUtil.getDate());
        UserAccount userAccount = accountManager.queryUserAccountByUserCode(userEntry.extendsInfo().get("user.accountCode")==null?"null":userEntry.extendsInfo().get("user.accountCode").get(0));
		serviceBean.setCreator(userAccount.getAccountCode());
		serviceManager.createService(serviceBean);

		ResultCall
				.makeSuccess("../servicecreate/queryServiceList.html?type="+serviceBean.getType());
	}

	/**
	 * 锟斤拷转锟斤拷锟睫改凤拷锟斤拷页锟斤拷
	 * 
	 * @param serviceID
	 */
	@RequestMapping
	public String gotoModService(ServiceBean serviceBean2, String pages,Map<String, Object> model) {
		ServiceBean serviceBean = serviceManager.queryServiceInfo(serviceBean2.getServiceID());
		model.put("opType", "2");
		model.put("serviceBean", serviceBean);
		model.put("serviceBean2", serviceBean2);
		model.put("type", serviceBean2.getType());
		model.put("pages", pages);
		return "service/createservice";
	}

	/**
	 * 锟睫改凤拷锟斤拷
	 * 
	 * @param servicebean
	 */
	@RequestMapping
	public void modService(ServiceBean servicebean,String serviceName2,String serviceType2,String status2,String createDate2,String pages) {
		serviceManager.modService(servicebean);
		ResultCall
				.makeSuccess("../servicecreate/queryServiceList.html?serviceType="
						+ serviceType2+"&type="+servicebean.getType()+"&serviceName="+serviceName2+"&status="+status2+"&createDate="+createDate2+"&pages="+pages);
	}

	/**
	 * 删锟斤拷锟斤拷锟�
	 * 
	 * @param servicebean
	 */
	@RequestMapping
	public void delService(String serviceID,String type) {
		serviceManager.delService(serviceID);
		ResultCall.makeSuccess("../servicecreate/queryServiceList.html?type="+type);
	}

	/**
	 * 
	 * @param serviceID
	 *            锟斤拷锟斤拷ID
	 * @param applicant
	 *            锟斤拷锟斤拷锟斤拷ID
	 */
	@RequestMapping
	public String gotoApplyService(@ModelAttribute("curUser") OperatorUserEntry userEntry,String serviceID,String type, Map<String, Object> model) {

	    if("3".equals(type))
		{
	        TutorBean tutorBean  = tutorManager.queryTutorById(serviceID);
		    model.put("tutorBean", tutorBean);
		}
	    else {
	        ServiceBean serviceBean = serviceManager.queryServiceInfo(serviceID);
	        model.put("serviceBean", serviceBean);
        }
	     
		//锟斤拷霉锟剿撅拷锟斤拷
		UserAccount userAccount = accountManager.queryUserAccountByUserCode(userEntry.extendsInfo().get("user.accountCode")==null?"null":userEntry.extendsInfo().get("user.accountCode").get(0));
		model.put("companyId", userAccount.getCompanyId());
		model.put("companyName", userAccount.getCompanyName());
		model.put("type",type);
		//2锟斤拷示锟斤拷請
		model.put("opType", "2");
		
		if("3".equals(type))
		{
		    return "service/querytutorinfo";
		}
		return "service/queryserviceinfo";
	}

	/**
	 * 
	 * @param serviceID
	 *            锟斤拷锟斤拷ID
	 * @param applicant
	 *            锟斤拷锟斤拷锟斤拷ID
	 */
	@RequestMapping
	public void applyService(
			@ModelAttribute("curUser") OperatorUserEntry userEntry,
			ServiceApplyBean serviceApplyBean, String needApproval) {
		serviceApplyBean.setApplicant(userEntry.userIdentifier());
		if ("1".equals(needApproval)) {
			serviceApplyBean.setStatus("1");
		} else {
			serviceApplyBean.setStatus("2");
		}
		UserAccount userAccount = accountManager.queryUserAccountByUserCode(userEntry.extendsInfo().get("user.accountCode")==null?"null":userEntry.extendsInfo().get("user.accountCode").get(0));
		serviceApplyBean.setCompanyId(userAccount.getCompanyId());
		serviceApplyBean.setApplicant(userAccount.getAccountCode());
		serviceApplyBean.setApplyTime(DateUtil.getNow());
		serviceManager.applyService(serviceApplyBean);
		ResultCall.makeSuccess("../servicecreate/queryServiceApplyList.html?type="+serviceApplyBean.getType());
	}

	/**
	 * 
	 * @param serviceID
	 *            锟斤拷锟斤拷ID
	 * @param applicant
	 *            锟斤拷锟斤拷锟斤拷ID
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping
	public void approveService(
			@ModelAttribute("curUser") OperatorUserEntry userEntry,
			ServiceApplyBean serviceApplyBean) throws UnsupportedEncodingException {
	     UserAccount userAccount = accountManager.queryUserAccountByUserCode(userEntry.extendsInfo().get("user.accountCode")==null?"null":userEntry.extendsInfo().get("user.accountCode").get(0));
	    if(serviceApplyBean.getArgument()!=null)
	    {
	     serviceApplyBean.setArgument(new String( serviceApplyBean.getArgument().getBytes("ISO8859_1"),"UTF-8"));
	    }
		serviceApplyBean.setApproval(userAccount.getAccountCode());
		serviceApplyBean.setApproveTime(DateUtil.getNow());
		serviceManager.approveService(serviceApplyBean);
		ResultCall.makeSuccess("../servicecreate/queryServiceApplyList.html?type="+serviceApplyBean.getType());
	}

	/**
	 * 
	 * @param serviceID
	 *            锟斤拷锟斤拷ID
	 * @param applicant
	 *            锟斤拷锟斤拷锟斤拷ID
	 */
	@RequestMapping
	public void delServiceApplyInfo(String id,String type) {
		serviceManager.delServiceApplyInfo(id);
		ResultCall.makeSuccess("../servicecreate/queryServiceApplyList.html?type="+type);
	}

	/**
     * 锟斤拷询锟斤拷锟斤拷锟叫憋拷
     */
    
    @RequestMapping
    public String queryServiceApplyList(
    		@ModelAttribute("curUser") OperatorUserEntry userEntry,
    		ServiceApplyBean serviceApplyBean, String pages,
    		Map<String, Object> model) {
    	// 锟斤拷锟斤拷锟斤拷锟揭碉拷屎牛锟街伙拷锟窖拷锟斤拷屎诺锟斤拷锟斤拷锟斤拷锟较�
    	String userType= accountManager.queryUserType(userEntry.userIdentifier());
    	if ("2".equals(userType)) {
    		serviceApplyBean.setCompanyId(accountManager.queryUserAccountByUserCode(userEntry.extendsInfo().get("user.accountCode")==null?"null":userEntry.extendsInfo().get("user.accountCode").get(0)).getCompanyId());
    	}
    
    	int count = serviceManager.countServiceApplyInfo(serviceApplyBean);
    
    	Pagination pagination = PageUtil.createPage(pages, count,
    			"../servicecreate/queryServiceApplyList.html");
    
    	// 锟接会话锟叫伙拷取锟矫伙拷锟斤拷锟斤拷
    	List<ServiceApplyBean> serviceApplyList = serviceManager
    			.queryServiceApplyList(serviceApplyBean,
    					pagination.getOffset(), pagination.getRowSize());
    
    	// 锟斤拷页锟斤拷锟斤拷
    	pagination.addCondition("applicant", serviceApplyBean.getApplicant());
    	pagination.addCondition("serviceType",
    			serviceApplyBean.getServiceType());
    	pagination.addCondition("status", serviceApplyBean.getStatus());
    	pagination.addCondition("applyTime", serviceApplyBean.getApplyTime());
    	pagination.addCondition("type", serviceApplyBean.getType());
    
    	model.put("serviceApplyList", serviceApplyList);
    	model.put("userType", userType);
    	model.put("serviceApplyBean", serviceApplyBean);
    	model.put(Navigation.PAGINATION, pagination);
    	model.put("type", serviceApplyBean.getType());
    	if("3".equals(serviceApplyBean.getType()))
    	{
    	    return "service/tutorapplyinfolist";
    	}
    	else {
    	    return "service/serviceapplyinfolist";
        }
    	
    }

   
	
	@RequestMapping
	public String queryMeetingApplyInfo(String serviceID,Map<String, Object> model)
	{
		List<ServiceApplyBean> servcieApplyList = serviceManager.queryMeetingApplyInfo(serviceID);
		String serviceName = serviceManager.queryServiceInfo(serviceID).getServiceName();
		Week week = new Week();
		
		
		model.put("servcieApplyList", servcieApplyList);
		model.put("week",week);
		model.put("serviceName",serviceName);
		return "service/queryMeetingApplyInfo";
		
	}

}
